<template>
  <div>
    <my-form-drawer
      :title="title"
      :width="450"
      :visible="visible"
      @ok="edit"
      @cancel="onClose"
    >
      <a-form-model ref="form" :model="form" :rules="rules">
        <a-form-model-item label="敏感词类型" prop="label">
          <a-input v-model="form.label" :maxLength="50" />
        </a-form-model-item>
        <a-form-model-item label="描述" prop="description">
          <a-textarea v-model="form.description" allow-clear />
        </a-form-model-item>
      
      </a-form-model>
    </my-form-drawer>
  </div>
</template>

<script>
import moment from "moment";
import { additem, updateitem } from "@/services/word";
import MyFormDrawer from "@/components/drawer/MyFormDrawer";

export default {
  name: "wordTpyeForm",
  components: { MyFormDrawer },
  data() {
    return {
      type: "0", //0添加，1编辑
      title: "",
      form: {
        dictId:'104',
        label: '',
        value:'',
        type:"sensitive",
        description:''
      },
      visible: false,
      rules: {
        label: [
          { required: true, message: "敏感词类型不能为空", trigger: "change" },
        ],
      },
      page: {
        currentPage: 1,
        pageSize: 10
      },
    };
  },
  computed: {},
  created() {
  },
  methods: {
    moment,
    initData() {
      this.form = {
        dictId:'104',
        word: "",
        wordType:'',
        sensitiveType:'BLACK'
      };
    },
    changeForm(state,action,data){
      this.visible = state;
      this.type = action;
      this.title = this.type ? "编辑敏感词" : "新增敏感词";
      if (this.type) {
        this.form = Object.assign(this.form,data);
        console.log(this.form);
      }
    },
    onClose() {
      this.visible = false;
      this.$refs["form"].clearValidate();
      this.initData();
    },
    edit() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        }
        if (this.type) {
          this.form.value = this.form.label
          updateitem(this.form).then(res => {
            console.log(res);
            if (res.data.code === 0) {
              this.$message.success("修改成功");
              this.onClose();
              this.$emit("getList");
            }
          });
        } else {
          this.form.value = this.form.label
          additem(this.form).then(res => {
            console.log(res);
            if (res.data.code === 0) {
              this.$message.success("添加成功");
              this.onClose();
              this.$emit("getList");
            }
          });
        }
      });
    },
  }
};
</script>

<style lang="less" scoped></style>
